package com.ncu.cloundeoffice.service.impl;

import com.ncu.cloundeoffice.mapper.TDepartmentMapper;
import com.ncu.cloundeoffice.pojo.ResponseResult;
import com.ncu.cloundeoffice.pojo.TDepartment;
import com.ncu.cloundeoffice.service.TDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TDepartmentServiceImpl implements TDepartmentService {

    @Autowired
    private TDepartmentMapper tDepartmentMapper;

    /**
     * 查找所有的部门
     * @return
     */
    @Override
    public List<TDepartment> getAllTDepartments() {
        return tDepartmentMapper.getAllTDepartments(-1);
    }

    /**
     * 添加部门
     * @param tDepartment
     * @return
     */
    @Override
    public ResponseResult addDep(TDepartment tDepartment) {
        tDepartment.setEnabled(true);
        tDepartmentMapper.addDep(tDepartment);
        if(tDepartment.getResult() == 1){
            return ResponseResult.success("添加成功",tDepartment);
        }
        return ResponseResult.error("添加失败");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteDep(int id) {
        TDepartment tDepartment = new TDepartment();
        tDepartment.setId(id);
        tDepartmentMapper.deleteDep(tDepartment);
        if(tDepartment.getResult() == -2){
            return ResponseResult.error("该部门下还有子部门,删除失败!");
        }else if(tDepartment.getResult() == -1){
            return ResponseResult.error("该部门下还有员工,删除失败!");
        }else if(tDepartment.getResult() == 1){
            return ResponseResult.success("删除成功!");
        }
        return ResponseResult.error("删除失败!");
    }

    /**
     * 查询所以部门 无索引方式
     * @return
     */
    @Override
    public List<TDepartment> getAllTDepartment() {
        return tDepartmentMapper.getAllTDepartment();
    }


}
